package com.github.pig.training.controller;
import java.util.List;
import java.util.Map;
import java.util.Date;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pig.common.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pig.common.constant.CommonConstant;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.training.entity.TrainingStudentRegister;
import com.github.pig.training.service.ITrainingStudentRegisterService;
import com.github.pig.common.web.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author andrew
 * @since 2018-06-26
 */
@RestController
@RequestMapping("/trainingStudentRegister")
public class TrainingStudentRegisterController extends BaseController {
    @Autowired private ITrainingStudentRegisterService trainingStudentRegisterService;

    /**
    * 通过ID查询
    *
    * @param id ID
    * @return TrainingStudentRegister
    */
    @GetMapping("/{id}")
    public R<TrainingStudentRegister> get(@PathVariable Integer id) {
        return new R<>(trainingStudentRegisterService.selectById(id));
    }


    /**
     * 通过ID查询
     *
     * @param studentId ID
     * @return TrainingStudentRegister
     */
    @GetMapping("/getObjByStudentId/{studentId}")
    public R<List<TrainingStudentRegister>> getObjByStudentId(@PathVariable Integer studentId) {
        Wrapper<TrainingStudentRegister> ew = new EntityWrapper();
        ew.where("student_id={0}",studentId).orderBy("create_time",false);
        trainingStudentRegisterService.selectList(ew);
        return new R<>(trainingStudentRegisterService.selectList(ew));
    }


    /**
    * 分页查询信息
    *
    * @param params 分页对象
    * @return 分页对象
    */
    @RequestMapping("/page")
    public Page page(@RequestParam Map<String, Object> params) {
        params.put(CommonConstant.DEL_FLAG, CommonConstant.STATUS_NORMAL);
        return trainingStudentRegisterService.selectPage(new Query<>(params), new EntityWrapper<>());
    }

    /**
     * 添加
     * @param  trainingStudentRegister  实体
     * @return success/false
     */
    @PostMapping
    public R<Boolean> add(@RequestBody TrainingStudentRegister trainingStudentRegister) {
        trainingStudentRegister.setCreateTime(new Date());
        trainingStudentRegister.setUpdateTime(new Date());
        trainingStudentRegister.setDelFlag(CommonConstant.STATUS_NORMAL);
        return new R<>(trainingStudentRegisterService.insert(trainingStudentRegister));
    }

    /**
     * 删除
     * @param id ID
     * @return success/false
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Integer id) {
        TrainingStudentRegister trainingStudentRegister = new TrainingStudentRegister();
        trainingStudentRegister.setFid(id);
        trainingStudentRegister.setUpdateTime(new Date());
        trainingStudentRegister.setDelFlag(CommonConstant.STATUS_DEL);
        return new R<>(trainingStudentRegisterService.updateById(trainingStudentRegister));
    }

    /**
     * 编辑
     * @param  trainingStudentRegister  实体
     * @return success/false
     */
    @PutMapping
    public R<Boolean> edit(@RequestBody TrainingStudentRegister trainingStudentRegister) {
        trainingStudentRegister.setUpdateTime(new Date());
        return new R<>(trainingStudentRegisterService.updateById(trainingStudentRegister));
    }

    /**
     * 添加学员并报名
     */
    @PostMapping("/signUp")
    public R<Boolean> signUp(@RequestBody Map<String,String> params, UserVO user){
        return trainingStudentRegisterService.signUp(params,user);
    }
}
